﻿using System;
using System.Windows.Forms;
using ChatClientApp.Classes;
using ChatServerApp.Classes;
using System.Drawing;
using System.Text.RegularExpressions;

namespace ChatClientApp.GUI
{
    public partial class fRegistration : Form
    {
        public fRegistration()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (canRegister())
            {
                User_Complete user = new User_Complete(tb_Name.Text, tb_Vorname.Text, tb_Email.Text, tb_Pw.Text, tb_Nickname.Text, tb_Jahrgang.Text);
                string srv_message = ServerHandling.Registration(user);
                if (srv_message.Equals("ACK"))
                {
                    MessageBox.Show("Registration successful");
                    this.Close();
                }
                else
                {
                    MessageBox.Show(srv_message);
                }
            }
            else
            {

            }
        }

        private void tb_Pw2_Leave(object sender, EventArgs e)
        {
            if (tb_Pw.Text != tb_Pw2.Text)
            {
                tb_Pw.BackColor = Color.Yellow;
                tb_Pw2.BackColor = Color.Yellow;
            }
            else
            {
                tb_Pw.BackColor = Color.White;
                tb_Pw2.BackColor = Color.White;
            }
        }

        private void tb_Pw_Leave(object sender, EventArgs e)
        {
            if (tb_Pw.Text != tb_Pw2.Text && tb_Pw2.Text != String.Empty)
            {
                tb_Pw.BackColor = Color.Yellow;
                tb_Pw2.BackColor = Color.Yellow;
            }
            else
            {
                tb_Pw.BackColor = Color.White;
                tb_Pw2.BackColor = Color.White;
            }
        }

        private bool isEmail(string inputEmail)
        {

            inputEmail = inputEmail == null ? "" : inputEmail;

            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\"   +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            Regex re = new Regex(strRegex);

            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);

        }

        private void tb_Email_Leave(object sender, EventArgs e)
        {
            if (isEmail(tb_Email.Text))
            {
                tb_Email.BackColor = Color.White;
            }
            else
            {
                tb_Email.BackColor = Color.Yellow;
            }
        }

        private bool canRegister()
        {
            // Nickname > 6
            // Email is valid
            // pw and pw 2 equal

            // if (tb_Nickname.Text.Length > 6 && ServerHandling.IsFreeNick(tb_Nickname.Text))
            if (ServerHandling.IsFreeNick(tb_Nickname.Text))
            {
                if (isEmail(tb_Email.Text))
                {
                    if (tb_Pw.Text == tb_Pw2.Text)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        private void tb_Nickname_Leave(object sender, EventArgs e)
        {
            if (ServerHandling.IsFreeNick(tb_Nickname.Text))
            {
                tb_Nickname.BackColor = Color.White;
            }
            else
            {
                tb_Nickname.BackColor = Color.Yellow;
            }
        }
    }
}
